package at.jaki.ubb.crawling.demo.dataTransformer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import at.jaki.ubb.crawling.api.DataTransformer;
import at.jaki.ubb.crawling.demo.model.StringEntity;
import at.jaki.ubb.crawling.model.DataTransformerResult;
import at.jaki.ubb.crawling.model.RawData;
import at.jaki.ubb.crawling.model.Target;
import at.jaki.ubb.crawling.stlib.dataFinder.BabylonTranslator;

public class ArticleKeywordExtractor implements DataTransformer<String, String, StringEntity, String> {

	private static final Logger LOG = Logger.getLogger(ArticleKeywordExtractor.class);

	private static final String KEYWORD_PATTERN = "[<]meta name[=][\"]keywords[\"][\\s\\S]*?content[=][\"](.*?)[\"][\\s][/][>]";
	private static final String TITLE_PATTERN = "[<]title[>](.*?)[<][/]title[>]";
	private static final Double PRIORITY = 0.9D;

	private static final List<String> filterOut = new ArrayList<String>();

	public DataTransformerResult<String, String, StringEntity> transformData(RawData<String> rawData) {

		List<String> keywords = getKeywords(rawData.getData());
		if (keywords.size() == 0) {
			keywords = getTitleWords(rawData.getData());
		}

		List<Target<String>> targetCollection = new ArrayList<Target<String>>(keywords.size());
		for (String keyword : keywords) {
			Target<String> target = new Target<String>(PRIORITY, keyword, BabylonTranslator.INPUT_TYPE, keyword);
			targetCollection.add(target);
		}
		LOG.info("extracted keywords: " + keywords);
		return new DataTransformerResult<String, String, StringEntity>(null, null, targetCollection);
	}

	private List<String> getKeywords(String data) {
		Pattern p = Pattern.compile(KEYWORD_PATTERN);
		Matcher matcher = p.matcher(data);

		List<String> keywords = new ArrayList<String>();
		if (matcher.find()) {
			String line = matcher.group(1);
			for (String s : line.split(",")) {
				if (s.length() > 1) {
					keywords.add(s.trim());
				}
			}
		}
		return keywords;
	}

	private List<String> getTitleWords(String data) {
		Pattern p = Pattern.compile(TITLE_PATTERN);
		Matcher matcher = p.matcher(data);

		List<String> titleWs = new ArrayList<String>();
		if (matcher.find()) {
			String line = matcher.group(1);
			for (String s : line.split("[ ]")) {
				s = s.replaceAll("[\\W]", "");
				s = s.trim();
				if (s.length() > 2 && !filterOut.contains(s.toLowerCase())) {
					titleWs.add(s);
				}
			}
		}
		return titleWs;
	}

	public void init() {
		filterOut.add("the");
		filterOut.add("one");
	}

	public List<String> getTypes() {
		return Collections.singletonList(PageClassifier.TYPE_OTHER_PAGE);
	}

}
